package com.ly.mpayment.rest.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.ly.mpayment.core.util.Config;
import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.core.util.StringUtils;
import com.ly.mpayment.core.util.UserUtil;
import com.ly.mpayment.persist.entity.User;
import com.ly.mpayment.persist.service.platformaccount.IPlatformAccountService;
import com.ly.mpayment.persist.service.platformaccountrecord.IPlatFormAccountRecordService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;

import net.sf.json.JSONObject;

@Component
@Path("system/plat/")
public class PlatRecordRest extends AbsRest {
	@Autowired
	IUserService userService;
	@Autowired
	IPlatFormAccountRecordService recordService;

	@Autowired
	IPlatformAccountService accountService;

	@POST
	@Path("findPlatRecord")
	public String findPlatRecord(String data, @Context HttpServletRequest request) {
		String result = this.validationParameter(data, request, userService, "start", "pageSize", "date", "name");
		if (result == null) {
			JSONObject json = JsonUtils.converToJson(data);
			Map<String, Object> parameters = MapUtil.getMap();
			if (!StringUtils.isBlank(json.getString("date"))) {
				parameters.put(":date", json.getString("date"));
			}
			if (!StringUtils.isBlank(json.getString("name"))) {
				parameters.put(":name", json.getString("name"));
			}
			return ResultMessage.getSucceed(
					JsonUtils.excludes(
							this.recordService.findPagePlatformaccountrecord(parameters, json.getInt("start"),
									json.getInt("pageSize")),
							new String[] { "platFormAccount", "errorCodesForUseruuid", "lastlogintime", "password",
									"token", "userstatus", "wechatid", "phonenum", "usertype", "useruuid" }));
		}
		return result;
	}

	@GET
	@Path("findPlatFromAccount")
	public String findPlatFromAccount(@Context HttpServletRequest request) {

		String token = request.getHeader("token");
		if (token != null) {
			DecodedJWT jwt;
			jwt = JWT.decode(token);
			if (jwt != null) {
				JSONObject jsonUser = JsonUtils.converToJson(jwt.getSubject());
				if (jsonUser != null) {
					User userAdmin = userService.findByUserid(jsonUser.getString(Config.tokenSignAttr));
					if(UserUtil.checkRole(userAdmin)){
						return ResultMessage.getSucceed( JsonUtils.excludes(accountService.findByAccount(),
								new String[]{"platformuuid","remark","downShops","platFormAccountRecords","upRechargeRecords",
										"userAccount","platformname","status","createtime","lastmodifytime"}));
					}
				}
			}
		}
		return ResultMessage.getFailt("");
	}

	/**
	 * 查询产品折扣
	 * @param data
	 * @param request
	 * @return
	 */
	@POST
	@Path("getProductDiscount")
	public String getProductDiscount(String data, @Context HttpServletRequest request) {
		String result = this.validationParameterGetRole(request, userService);
		if (result == null) {
			return ResultMessage.getSucceed(recordService.getProductDiscount(data));
		}
		return result;
	}
}
